package com.example.sea.back.web;

import com.alibaba.druid.util.StringUtils;
import com.example.sea.back.common.context.SessionKeyContext;
import com.example.sea.core.common.utils.VerificationCodeUtil;
import org.apache.shiro.authc.DisabledAccountException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Controller
public class LoginController {

    private Logger log=LoggerFactory.getLogger(LoginController.class);

    /**
     * 登录页面
     * @param request
     * @param error
     * @return
     */
    @GetMapping("/login")
    public String loginHtml(HttpServletRequest request,String error){
        getLoginError(request, error);
        return "login";
    }

    /**
     * 验证码
     * @param request
     * @return
     */
    @ResponseBody
    @GetMapping("/randomCode")
    public String randomCode(HttpServletRequest request){
        Map<String, String> map = VerificationCodeUtil.create();
        String randomCodeKey = map.get("randomCodeKey");
        request.getSession().setAttribute(SessionKeyContext.SESSION_RANDOM_CODE_KEY,map.get("randomCodeValue"));
        return randomCodeKey;
    }

    /**
     * 配置错误信息
     * @param request
     * @param error
     */
    private void getLoginError(HttpServletRequest request, String error) {
        if (!StringUtils.isEmpty(error)){
            if (error.equals("UnknownAccountException")){
                request.setAttribute("error","账号不存在");
            }else if(error.equals("IncorrectCredentialsException")){
                request.setAttribute("error","密码错误");
            }else if(error.equals("RandomCodeException")){
                request.setAttribute("error","验证码错误");
            }else if(error.equals("DisabledAccountException")){
                request.setAttribute("error","账户被冻结");
            }else{
                request.setAttribute("error","登录失败");
            }
        }
    }

    /**
     * 登录提交返回错误
     * @param request
     * @return
     * @throws Exception
     */
    @PostMapping("/login")
    public String loginSubmit(HttpServletRequest request) throws Exception{
        String exception = (String) request.getAttribute("shiroLoginFailure");
        String error="?error=";
        if (exception != null) {
            if (UnknownAccountException.class.getName().equals(exception)) {
                error+="UnknownAccountException";
            } else if (IncorrectCredentialsException.class.getName().equals(exception)) {
                error+="IncorrectCredentialsException";
            } else if ("RandomCodeException".equals(exception)) {
                error+="RandomCodeException";
            } else if (DisabledAccountException.class.getName().equals(exception)){
                error+="DisabledAccountException";
            } else {
                error+="Exception";
            }
        }

        return "redirect:/login"+error;
    }
}
